from kivy.logger import Logger
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader
from kivy.metrics import dp

from dryfire.view.calibrationsView import CalibrationsView
from dryfire.view.rangesView import RangesView
from dryfire.view.splashView import SplashView
from dryfire.view.targetView import TargetView

class AppView(TabbedPanel):
    def __init__(self, **kwargs):
        TabbedPanel.__init__(self, **kwargs)
        self.do_default_tab = True
        self.tab_width = dp(200)
        self.bind(_current_tab=self._handle_on_current_tab)
        # splash view
        self.default_tab_text = 'dryfire'
        self.default_tab_content = SplashView(size_hint=(1.0, 1.0))
        # calibrate view
        self._calibrate_header = TabbedPanelHeader(text='Calibrate')
        self._calibrate_header.content = CalibrationsView(size_hint=(1.0, 1.0))
        self.add_widget(self._calibrate_header)
        # setup view
        self._setup_header = TabbedPanelHeader(text='Setup')
        self._setup_header.content = RangesView(size_hint=(1.0, 1.0))
        self.add_widget(self._setup_header)
        # target view
        self._target_header = TabbedPanelHeader(text='Practice')
        self._target_header.content = TargetView(size_hint=(1.0, 1.0))

    def show_target(self):
        if self.current_tab is not self._target_header:
            if self._target_header not in self.tab_list:
                self.add_widget(self._target_header)
            self.switch_to(self._target_header)
            
    def show_default_tab(self):
        if self.current_tab is not self.default_tab:
            self.switch_to(self.default_tab)

    def _handle_on_current_tab(self, instance, value):
        # instance is self
        # value is the tabbed panel header
        # set the target view's visibility
        th = self._target_header
        th.content.is_visible = (value == th)
        # if the target view is not the current tab then remove it.
        if not th.content.is_visible: 
            if th in self.tab_list:
                self.remove_widget(th)
